package model.visibility;

import java.util.List;

import model.map.GameMap;
import model.map.GameTile;
import model.playable.structure.Structure;
import model.playable.unit.Unit;
import model.playable.unit.Vehicle;
import model.player.Player;

public class VisibilityManager implements PlayerView 
{
	private GameMap map;
	private Player player;
	private VisibleMap visibleMap;
	
	public VisibilityManager(GameMap map, Player p)
	{
		this.map = map;
		this.player = p;
		this.visibleMap = new VisibleMap(); 
	}
	
	public void build(TileBuilder builder) 
	{
		visibleMap.build(map.getTileAt(builder.getCoordX(), builder.getCoordY()), builder);
		
	}

	public int getMapHeight() {
		return map.getMapHeight();
	}

	public int getMapLength() {
		return map.getMapLength();
	}

	@Override
	public int getPlayerEnergy() 
	{
		return player.getEnergyCount();
	}

	@Override
	public int getPlayerFood()
	{
		return player.getFoodCount();
	}

	@Override
	public int getPlayerOre() 
	{
		return player.getOreCount();
	}

	@Override
	public void giveInitialTileInfo(TileBuilder builder)
	{
		visibleMap.giveTileInfo(map.getTileAt(builder.getCoordX(), builder.getCoordY()), builder);
		
	}
	
	public void update()
	{
		
		GetVisibleTilesFunction visTileFunction = new GetVisibleTilesFunction();
		
		List<Unit> units = player.getAllUnits();
		
		for (Unit u : units)
		{
			visTileFunction.executeOnUnit(u);
		}
		
		List<Structure> structures = player.getAllStructures();
		
		for (Structure s : structures)
		{
			visTileFunction.executeOnStructure(s);
		}
		
		List<Vehicle> vehicles = player.getAllVehicles();
		
		for (Vehicle v : vehicles)
			visTileFunction.executeOnVehicle(v);
		
		visibleMap.update(visTileFunction.getVisibleTiles());
	}
	
	public void prospect(GameTile tile)
	{
		visibleMap.revealResources(tile);
	}
}
